package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.dto.PageDto;
import com.qf.pojo.DtsArticle;
import com.qf.service.DtsArticleService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/25 10:24
 */

@RestController
@RequestMapping("/admin/article")
@Slf4j
@CrossOrigin
public class ArticleController {
@Autowired
private DtsArticleService dtsArticleService;

    //http://localhost:8080/admin/article/list?page=1&limit=20&title=kkk&sort=add_time&order=desc
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button ="查找")
    @RequiresPermissions("admin:article:list")
    @GetMapping("/list")
    public  Object articleList(PageDto pageDto){
        if(pageDto==null){
            return ResponseUtil.badArgumentValue();
        }
        Page<DtsArticle>  dtsArticlePage= dtsArticleService.selectPage(pageDto);

        List<DtsArticle> records = dtsArticlePage.getRecords();
        long total = dtsArticlePage.getTotal();


        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);

    }
   // http://localhost:8080/admin/article/detail?id=1

    @GetMapping("/detail")
    public  Object detailArticle(Integer id){
        if(id==null){
            return ResponseUtil.badArgumentValue();
        }
        DtsArticle dtsArticle=  dtsArticleService.selectDetail(id);
        return  ResponseUtil.ok(dtsArticle);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button ="编辑")
    @RequiresPermissions("admin:article:update")
    @PostMapping("/update")
    public  Object updateArticle(@RequestBody DtsArticle dtsArticle){
        if(dtsArticle==null){
            return ResponseUtil.badArgumentValue();
        }
         dtsArticle.setUpdateTime(new Date());
         dtsArticleService.update(dtsArticle);
         return ResponseUtil.ok(dtsArticle);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button ="添加")
    @RequiresPermissions("admin:article:create")
    @PostMapping("/create")
    public  Object createArticle(@RequestBody DtsArticle dtsArticle){
        if(dtsArticle==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsArticle.setUpdateTime(new Date());
        dtsArticle.setAddTime(new Date());
        dtsArticle.setDeleted(false);
        dtsArticleService.insert(dtsArticle);
        return ResponseUtil.ok(dtsArticle);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button ="删除")
    @RequiresPermissions("admin:article:delete")
    @PostMapping("/delete")
    public  Object deleteArticle(@RequestBody DtsArticle dtsArticle){
        if(dtsArticle==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsArticle.setUpdateTime(new Date());
        dtsArticle.setDeleted(true);
        dtsArticleService.delete(dtsArticle);
        return ResponseUtil.ok();
    }
    //http://localhost:8080/admin/groupon/list?page=1&limit=20&sort=add_time&order=desc
}
